
package com.borqs.music.tasks;

import static com.borqs.music.http.RequestMethod.POST;
import static com.borqs.music.util.MusicConf.ReqType.MUSIC_UPLOAD_SONG;

import java.io.File;
import java.util.HashMap;
import java.util.Map;

import com.borqs.music.executor.Task;
import com.borqs.music.executor.TaskListener;
import com.borqs.music.http.HttpRequest;
import com.borqs.music.http.HttpResponse;
import com.borqs.music.model.SongInfo;
import com.borqs.music.util.MusicConf;

public class UploadTask extends Task {
    private SongInfo song;
    private String ticket;

    public UploadTask(TaskListener listener, SongInfo song, String ticket) {
        super(listener);
        this.song = song;
        this.ticket = ticket;
    }

    @Override
    public HttpRequest prepareRequest() {
        // file parameters
        File music = new File(song.fileUrl);
        Map<String, File> fileMap = new HashMap<String, File>(1);
        fileMap.put("music", music);

        // normal parameters
        Map<String, String> params = new HashMap<String, String>(5);
        params.put("filename", song.fileName);
        params.put("title", song.title);
        params.put("singer", song.artist);
        params.put("album", song.album);
        if (song.year != null) {
            params.put("year", song.year);
        }
        params.put("duration", "" + song.duration);
        params.put("md5", song.fileMd5);

        final String URL = MusicConf.getUrl(MUSIC_UPLOAD_SONG);
        HttpRequest req = new HttpRequest(POST, URL, ticket, params, fileMap);

        return req;
    }

    @Override
    public Object handleResponse(HttpResponse resp) throws Exception {
        checkResponse(resp);
        return song;
    }
}
